

// 目前没有window电脑，还不能看到文档，以下都是通过其他资料里找到的

import RxRequest_Base from './RxRequest_Base.js'

// 获取本地配置 - get缓存
RxRequest_Base.getLocalCache = () => {
  return new Promise((resolve) => {
    const res = LanRenJingLing.getCache()
    resolve(res)
  })
}

// 保存本地配置 - save缓存
RxRequest_Base.saveLocalCache = (data = {}) => {
  return new Promise((resolve) => {
    LanRenJingLing.saveCache(data)
    resolve()
  })
}

RxRequest_Base.cancel = () => {
  // 没有返回值
  LanRenJingLing.cancel()
}

RxRequest_Base.confirm = (data = {}) => {
  // 没有返回值
  LanRenJingLing.ok(data)
}

export default RxRequest_Base


/**
 * 触摸精灵
 *
 * 使用  touchelf.return()为触摸内置函数
 */

const LanRenJingLing = {
  cacheKey: 'RxLanRenCache',
  configFileName: 'RxLRConfig.txt',
  configPath: ''
}

LanRenJingLing.cancel = () => {
  touchelf.return(JSON.stringify({ type: "cancel" }))
}

LanRenJingLing.ok = (data = {}) => {
  window.bridge.confirm()
}

LanRenJingLing.getCache = () => {
  let result = {}
  try {
    result = localStorage.getItem(LanRenJingLing.cacheKey);
  } catch (error) {
    window.bridge.toast(error.message, 20)
    console.error('LanRenJingLing.getCache error', error)
  }
  return result
}

LanRenJingLing.saveCache = (data = {}) => {
  try {
    if (!LanRenJingLing.configPath) {
      LanRenJingLing.configPath = window.bridge.getWorkPath() + '/' + LanRenJingLing.configFileName
    }

    const str = JSON.stringify(data)
    window.bridge.writeStringToFile(LanRenJingLing.configPath, str)
  } catch (error) {
    window.bridge.toast(error.message, 20)
    console.error('LanRenJingLing.saveCache error', error)
  }
}
